using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace InvadersEx02.ObjectModel
{
    public class Sprite : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Texture2D m_Texture;
        private Vector2 m_Position;
        private Vector2 m_Velocity;
        private Color m_TintColor;
        private string m_AssetName;
        private float m_Width;
        private float m_Height;
        private bool m_UseSharedBatch;
        protected SpriteBatch m_SpriteBatch;

        public Sprite(Game game, string i_AssetName)
            : base(game)
        {
            m_AssetName = i_AssetName;
            m_Position = Vector2.Zero;
            m_TintColor = Color.White;
            m_UseSharedBatch = true;
            m_SpriteBatch = null;
            m_Width = 0;
            m_Height = 0;
        }

        public Vector2 Position
        {
            get { return m_Position; }
            set { m_Position = value; }
        }

        public Vector2 Velocity
        {
            get { return m_Velocity; }
            set { m_Velocity = value; }
        }

        public Color TintColor
        {
            get { return m_TintColor; }
            set { m_TintColor = value; }
        }

        public SpriteBatch SpriteBatch
        {
            set
            {
                m_SpriteBatch = value;
                m_UseSharedBatch = true;
            }
        }

        public float Width
        {
            get { return m_Width; }
        }

        public float Height
        {
            get { return m_Height; }
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            m_Texture = Game.Content.Load<Texture2D>(m_AssetName);
            
            m_Width = m_Texture.Width;
            m_Height = m_Texture.Height;

            if (m_SpriteBatch == null)
            {
                m_SpriteBatch = new SpriteBatch(Game.GraphicsDevice);
                m_UseSharedBatch = false;
            }

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            this.m_Position += this.m_Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (!m_UseSharedBatch)
            {
                m_SpriteBatch.Begin();
            }

            m_SpriteBatch.Draw(m_Texture, m_Position, m_TintColor);

            if (!m_UseSharedBatch)
            {
                m_SpriteBatch.End();
            }

            base.Draw(gameTime);
        }
    }
}
